package com.ryan.spring.annotation.ext;

import com.ryan.spring.annotation.model.Person;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

/**
 * Created by kaimin on 14/2/2019.
 * time : 22:52
 */
@Component
public class MyBeanDefinitionPostProcessor implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        //
        AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Person.class).getBeanDefinition();
        registry.registerBeanDefinition("person",beanDefinition);
        System.out.println("com.ryan.spring.annotation.ext.MyBeanDefinitionPostProcessor.postProcessBeanDefinitionRegistry 的数量"+registry.getBeanDefinitionCount());
    }

    //BeanDefinitionRegistry中保存了每一个bean定义的信息
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("com.ryan.spring.annotation.ext.MyBeanDefinitionPostProcessor.postProcessBeanFactory 的数量"+beanFactory.getBeanDefinitionCount());
    }
}
